package com.bsj.sipgateway.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.cached.RedisCached;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.constant.AlarmEnum;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.AlarmCombinationRDS;
import com.bsj.power.common.def.dto.AlarmQueryDTO;
import com.bsj.power.common.def.dto.PageDTO;
import com.bsj.power.common.def.dto.alarmReport.AlarmHandleDTO;
import com.bsj.power.common.def.dto.video.FaultRecordDTO;
import com.bsj.power.common.def.entity.AlarmCombination;
import com.bsj.power.common.def.entity.Scene;
import com.bsj.power.common.def.entity.alarmReport.AlarmReport;
import com.bsj.power.common.def.entity.gb28181.Device;
import com.bsj.power.common.def.vo.AlarmCombinationVO;
import com.bsj.power.common.def.vo.web.system.ai.AnnexStatVO;
import com.bsj.power.common.def.vo.web.system.ai.count.AnnexAreaNum;
import com.bsj.power.common.def.vo.web.system.ai.count.AnnexDayNum;
import com.bsj.power.common.def.vo.web.system.ai.count.AnnexTypeNum;
import com.bsj.power.common.mapper.AlarmComMapper;
import com.bsj.power.common.mapper.AlarmReportMapper;
import com.bsj.power.common.mapper.DeviceMapper;
import com.bsj.power.common.mapper.SceneMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.common.util.ParamCheckUtil;
import com.bsj.power.common.util.StringUtils;
import com.bsj.sipgateway.web.service.IAlarmService;
import com.bsj.sipgateway.web.service.IDeviceService;
import com.bsj.sipgateway.web.service.ISceneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.bsj.power.common.util.DateUtils.yearMonthDay;

/**
 * @Description
 * @Author zhaoyang
 * @Date 2024/7/2 11:37
 */
@Slf4j
@Service("alarmServiceImpl")
public class AlarmServiceImpl implements IAlarmService {

    @Autowired
    private AlarmReportMapper alarmReportMapper;

    @Autowired
    private AlarmComMapper alarmComMapper;

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SceneMapper sceneMapper;

    @Autowired
    @Qualifier("deviceServiceImpl")
    private IDeviceService iDeviceService;

    @Autowired
    @Qualifier("sceneServiceImpl")
    private ISceneService sceneService;

    @Autowired
    private RedisCached redisCached;


    @Override
    public List<AlarmReport> faultRecord(FaultRecordDTO faultRecordDTO) {
        String vagueStr = faultRecordDTO.getVagueStr();
        String beginTime = faultRecordDTO.getBeginTime();
        String endTime = faultRecordDTO.getEndTime();
//        Integer timeType = faultRecordDTO.getTimeType();
        Integer pageNumber = faultRecordDTO.getPageNumber();
        Integer pageSize = faultRecordDTO.getPageSize();
        Date date = new Date();
//        if (beginTime == null || endTime == null) { // 如果没有开始时间或者结束时间，就用选定的时间类型
//            if (timeType == null) {
//                throw new ApiException(JsonResultEnum.PARAM_ERROR);
//            }
//            switch (timeType) {
//                case 1: // 今天
//                    beginTime = DateUtils.getStartOfDay2(date);
//                    endTime = DateUtils.getEndOfDay2(date);
//                    break;
//                case 2: // 昨天
//                    beginTime = DateUtils.getStartOfYesterday();
//                    endTime = DateUtils.getEndOfYesterday();
//                    break;
//                case 3: // 本周
//                    beginTime = DateUtils.getStartOfThisWeek();
//                    endTime = DateUtils.getEndOfThisWeek();
//                    break;
//                case 4: // 本月
//                    beginTime = DateUtils.getStartOfThisMonth();
//                    endTime = DateUtils.getEndOfThisMonth();
//                    break;
//                default:
//                    log.warn("时间类型判断错误");
//                    break;
//            }
//        }
        IPage<AlarmReport> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<AlarmReport> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vagueStr)) {
            queryWrapper.like("nike_name", vagueStr).or().like("device_name", vagueStr);
        }
        queryWrapper.like("alarm_method", 2).or().like("alarm_method", 6);
        queryWrapper.between("begin_time", beginTime, endTime);
        IPage<AlarmReport> result = alarmReportMapper.selectPage(page, queryWrapper);
        return result.getRecords();
    }

    @Override
    public List<AlarmReport> findFaultAlarmByTime(String beginTime, String endTime) {
        QueryWrapper<AlarmReport> wrapper = new QueryWrapper<>();
        // alarmMethod是2或者6的属于设备故障报警
        wrapper.like("alarm_method", 2).or().like("alarm_method", 6);
        wrapper.between("begin_time", beginTime, endTime);
        List<AlarmReport> alarmReports = alarmReportMapper.selectList(wrapper);
        return alarmReports;
    }


    @Override
    public IPage<AlarmReport> pageList(PageDTO dto, Integer alarmCode, Integer sceneId, String deviceName
            , Integer sortType, Integer alarmLevel) {
        // 根据传入的参数，确定queryWrapper的查询参数
        IPage<AlarmReport> alarmReportIPage = new Page<>(dto.getPageNumber(), dto.getPageSize());
        QueryWrapper<AlarmReport> queryWrapper = new QueryWrapper<>();
        // 1、根据传入的场景或设备id确认是否需要in ids(sceneId!=null)或者eq id(deviceName!=null)或者不需要这个参数(两个都为null)
        if (sceneId != null) {
            //查询当前场景的所有下级场景
            List<Integer> sceneIds = sceneService.getSceneIds(sceneId, NumericalConstants.TWO);
            List<Device> devices = iDeviceService.selectBySceneIds(sceneIds);
            if (!CollectionUtils.isEmpty(devices)) {
                List<String> deviceIds = devices.stream().map(Device::getDeviceId).collect(Collectors.toList());
                queryWrapper.in("device_name", deviceIds);
            }
        } else if (deviceName != null) {
            queryWrapper.eq("device_name", deviceName);
        }

        // 2、根据传入的alarmCode和alarmLevel确定最终需要查询的alarmCode类型
        List<Integer> codes = AlarmEnum.getAICameraAlarmCodes();
        Map<String, String> stringStringMap = redisCached.hgetAll(RedisConstant.ALARM_LEVEL_KEY);
        // 报警级别对应的报警code集合
        Map<Integer, List<Integer>> levelCodesMap = new HashMap<>();
        for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            levelCodesMap.put(Integer.parseInt(key)
                    , (List<Integer>) JSONObject.parseObject(value, List.class));
        }
        List<Integer> level1Codes = levelCodesMap.get(1);
        List<Integer> level2Codes = levelCodesMap.get(2);
        if (alarmCode == null && alarmLevel == null) {
            queryWrapper.in("alarm_type", codes);
        } else if (alarmCode == null) {
            if (alarmLevel == 0) { // 如果查询普通报警，就去掉1级和2级
                codes.removeAll(level1Codes);
                codes.removeAll(level2Codes);
            } else if (alarmLevel == 1) { // 如果查询1级或者2级报警，找交集
                codes.retainAll(level1Codes);
            } else if (alarmLevel == 2) {
                codes.retainAll(level2Codes);
            }
            queryWrapper.in("alarm_type", codes);
        } else if (alarmLevel == null) {
            queryWrapper.eq("alarm_type", alarmCode);
        } else {
            if (alarmLevel == 0) { // 如果查询普通报警，就去掉1级和2级
                codes.removeAll(level1Codes);
                codes.removeAll(level2Codes);
            } else if (alarmLevel == 1) { // 如果查询1级或者2级报警，找交集
                codes.retainAll(level1Codes);
            } else if (alarmLevel == 2) {
                codes.retainAll(level2Codes);
            }
            if (!codes.contains(alarmCode)) {
                throw new ApiException(JsonResultEnum.ALARM_NAME_AND_ALARM_LEVEL_NOT_MATCH);
            } else {
                queryWrapper.eq("alarm_type", alarmCode);
            }
        }
        // 3、时间
        queryWrapper.between("begin_time", dto.getStartTime(), dto.getEndTime());
        // 4、排序
        if (sortType == 1) {
            //升序
            queryWrapper.orderByAsc("begin_time");
        } else {
            //逆序
            queryWrapper.orderByDesc("begin_time");
        }

        IPage<AlarmReport> result = alarmReportMapper.selectPage(alarmReportIPage, queryWrapper);
        // 添加设备报警级别字段
        if (result != null && !CollectionUtils.isEmpty(result.getRecords())) {
            for (AlarmReport record : result.getRecords()) {
                Integer alarmType = record.getAlarmType();
                if (level1Codes.contains(alarmType)) {
                    record.setAlertLevel(1);
                } else if (level2Codes.contains(alarmType)) {
                    record.setAlertLevel(2);
                } else {
                    record.setAlertLevel(0);
                }
            }
        }

        return result;

    }


    @Override
    public List<AlarmReport> selectList(PageDTO dto, Integer alarmCode, Integer sceneId, String deviceName, Integer sortType, Integer alarmLevel) {
        // 根据传入的参数，确定queryWrapper的查询参数
        QueryWrapper<AlarmReport> queryWrapper = new QueryWrapper<>();
        // 1、根据传入的场景或设备id确认是否需要in ids(sceneId!=null)或者eq id(deviceName!=null)或者不需要这个参数(两个都为null)
        if (sceneId != null) {
            //查询当前场景的所有下级场景
            List<Integer> sceneIds = sceneService.getSceneIds(sceneId, NumericalConstants.TWO);
            List<Device> devices = iDeviceService.selectBySceneIds(sceneIds);
            if (!CollectionUtils.isEmpty(devices)) {
                List<String> deviceIds = devices.stream().map(Device::getDeviceId).collect(Collectors.toList());
                queryWrapper.in("device_name", deviceIds);
            }
        } else if (deviceName != null) {
            queryWrapper.eq("device_name", deviceName);
        }

        // 2、根据传入的alarmCode和alarmLevel确定最终需要查询的alarmCode类型
        List<Integer> codes = AlarmEnum.getAICameraAlarmCodes();
        Map<String, String> stringStringMap = redisCached.hgetAll(RedisConstant.ALARM_LEVEL_KEY);
        // 报警级别对应的报警code集合
        Map<Integer, List<Integer>> levelCodesMap = new HashMap<>();
        for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            levelCodesMap.put(Integer.parseInt(key)
                    , (List<Integer>) JSONObject.parseObject(value, List.class));
        }
        List<Integer> level1Codes = levelCodesMap.get(1);
        List<Integer> level2Codes = levelCodesMap.get(2);
        if (alarmCode == null && alarmLevel == null) {
            queryWrapper.in("alarm_type", codes);
        } else if (alarmCode == null) {
            if (alarmLevel == 0) { // 如果查询普通报警，就去掉1级和2级
                codes.removeAll(level1Codes);
                codes.removeAll(level2Codes);
            } else if (alarmLevel == 1) { // 如果查询1级或者2级报警，找交集
                codes.retainAll(level1Codes);
            } else if (alarmLevel == 2) {
                codes.retainAll(level2Codes);
            }
            queryWrapper.in("alarm_type", codes);
        } else if (alarmLevel == null) {
            queryWrapper.eq("alarm_type", alarmCode);
        } else {
            if (alarmLevel == 0) { // 如果查询普通报警，就去掉1级和2级
                codes.removeAll(level1Codes);
                codes.removeAll(level2Codes);
            } else if (alarmLevel == 1) { // 如果查询1级或者2级报警，找交集
                codes.retainAll(level1Codes);
            } else if (alarmLevel == 2) {
                codes.retainAll(level2Codes);
            }
            if (!codes.contains(alarmCode)) {
                throw new ApiException(JsonResultEnum.ALARM_NAME_AND_ALARM_LEVEL_NOT_MATCH);
            } else {
                queryWrapper.eq("alarm_type", alarmCode);
            }
        }
        // 3、时间
        queryWrapper.between("begin_time", dto.getStartTime(), dto.getEndTime());
        // 4、排序
        if (sortType == 1) {
            //升序
            queryWrapper.orderByAsc("begin_time");
        } else {
            //逆序
            queryWrapper.orderByDesc("begin_time");
        }

        List<AlarmReport> result = alarmReportMapper.selectList(queryWrapper);
        // 添加设备报警级别字段
        if (result != null && !CollectionUtils.isEmpty(result)) {
            for (AlarmReport record : result) {
                Integer alarmType = record.getAlarmType();
                if (level1Codes.contains(alarmType)) {
                    record.setAlertLevel(1);
                } else if (level2Codes.contains(alarmType)) {
                    record.setAlertLevel(2);
                } else {
                    record.setAlertLevel(0);
                }
            }
        }

        return result;
    }

    @Override
    public AnnexStatVO analysisAiAnnex(String startTime, String endTime) {
        Date start = null;
        Date end = null;
        if (startTime != null && endTime != null) {
            start = DateUtils.strToDate(startTime);
            end = DateUtils.strToDate(endTime);
        } else {
            //没有时间查询条件则默认查询近七天
            end = new Date();
            start = DateUtils.nDaysAgo(LocalDateTime.now(), 6);
        }
        QueryWrapper<AlarmReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_type", 4);
        queryWrapper.between("begin_time", start, end);

        // 不统计设备异常离线报警的
        queryWrapper.notIn("alarm_type", AlarmEnum.DEVICE_ABNORMAL_OFFLINE.getCode());
        //获取符合要求的数据
        List<AlarmReport> alarms = alarmReportMapper.selectList(queryWrapper);
        AnnexStatVO vo = new AnnexStatVO();
        if (alarms.isEmpty()) {
            return vo;
        }

        // 按照报警类型分类
        List<AnnexTypeNum> annexTypeNums = new ArrayList<>();
        Map<Integer, List<AlarmReport>> alarmTypeMap = alarms.stream().collect(Collectors.groupingBy(AlarmReport::getAlarmType));
        for (Map.Entry<Integer, List<AlarmReport>> entry : alarmTypeMap.entrySet()) {
            Integer key = entry.getKey();
            List<AlarmReport> value = entry.getValue();
            AnnexTypeNum annexTypeNum = AnnexTypeNum.builder().build();
            String nameByCode = AlarmEnum.getNameByCode(key);
            annexTypeNum.setType(nameByCode);
            annexTypeNum.setCount(value.size());
            annexTypeNums.add(annexTypeNum);
        }

        vo.setAnnexTypeNums(annexTypeNums);

        //计算每日的报警附件数量
        List<AnnexDayNum> annexDayNums = new ArrayList<>();
        //根据开始时间和结束时间计算近几日作业列表
        List<String> dateAfter = DateUtils.getDateAfter(startTime, endTime);
        SimpleDateFormat sdf = new SimpleDateFormat(yearMonthDay);
        dateAfter.sort((o1, o2) -> {
            try {
                return sdf.parse(o1).compareTo(sdf.parse(o2));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        for (String dateToStr : dateAfter) {
            Date day = DateUtils.strToDate(yearMonthDay, dateToStr);
            // 获取当前天开始和结束的时间
            Date currentStart = DateUtils.getStartOfDay(day);
            Date currentEnd = DateUtils.getEndOfDay(day);
            //当前天所有的报警数
            long dayCount = alarms.stream().filter(item -> item.getBeginTime().getTime() >= currentStart.getTime()
                    && item.getBeginTime().getTime() <= currentEnd.getTime()).count();
//            String dateToStr = DateUtils.dateToStr(yearMonthDay, day);
            annexDayNums.add(AnnexDayNum.builder()
                    .time(dateToStr)
                    .num(dayCount)
                    .build());
        }
        vo.setAnnexDayNums(annexDayNums);

        //按区域统计附件排行数量
        List<AnnexAreaNum> annexAreaNums = new ArrayList<>();
        // 获取所有的报警设备的id
        Map<String, List<AlarmReport>> collect = alarms.stream().collect(Collectors.groupingBy(AlarmReport::getDeviceName));
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.in("device_id", collect.keySet());
        // 获取所有的报警设备，关联了场景
        List<Device> devices = deviceMapper.selectList(deviceQueryWrapper);
        List<Integer> sceneIds = devices.stream().mapToInt(Device::getSceneId).boxed().collect(Collectors.toList());
        // 场景下的设备列表
        Map<Integer, List<Device>> sceneIdDevices = devices.stream().collect(Collectors.groupingBy(Device::getSceneId));

        List<Scene> scenes = sceneMapper.selectBatchIds(sceneIds);

        for (Scene scene : scenes) {
            Integer id = scene.getId();
            List<Device> devices1 = sceneIdDevices.get(id);
            long count = 0;
            for (Device device : devices1) {
                String deviceId = device.getDeviceId();
                List<AlarmReport> alarmReports = collect.get(deviceId);
                if (!CollectionUtils.isEmpty(alarmReports)) {
                    count += alarmReports.size();
                }
            }

            AnnexAreaNum annexAreaNum = AnnexAreaNum.builder()
                    .areaName(scene.getSceneName())
                    .num(count)
                    .build();
            annexAreaNums.add(annexAreaNum);
        }
        //让数据好看将数据进行排序(倒序)
        annexAreaNums.sort(Comparator.comparingLong(AnnexAreaNum::getNum).reversed());
        vo.setAnnexAreaNums(annexAreaNums);
        return vo;
    }

    @Override
    public void alarmComAdd(AlarmCombination alarmCombination) {
        String comName = alarmCombination.getComName();
        QueryWrapper<AlarmCombination> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_name", comName);
        AlarmCombination sameName = alarmComMapper.selectOne(queryWrapper);
        if (sameName != null) {
            throw new ApiException(JsonResultEnum.DEVICE_NAME_EXIST, "组合名称已经存在");
        }

        int insert = alarmComMapper.insert(alarmCombination);
        if (insert > 0) {
            AlarmCombinationRDS alarmCombinationRDS = new AlarmCombinationRDS();
            BeanUtils.copyProperties(alarmCombination, alarmCombinationRDS);
            jedisClusterBSJ.hset(RedisConstant.ALARM_COMBINATION
                    , alarmCombination.getId() + "", JSON.toJSONString(alarmCombinationRDS));
        }
    }

    @Override
    public void alarmComUpdate(AlarmCombination alarmCombination) {
        String comName = alarmCombination.getComName();
        String comCode = alarmCombination.getComCode();
        QueryWrapper<AlarmCombination> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_name", comName);
        AlarmCombination sameName = alarmComMapper.selectOne(queryWrapper);
        if (sameName != null && !sameName.getId().equals(alarmCombination.getId())) {
            throw new ApiException(JsonResultEnum.DEVICE_NAME_EXIST, "组合名称已经存在");
        }

        UpdateWrapper<AlarmCombination> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", alarmCombination.getId());

        if (!StringUtils.isEmpty(comName)) {
            updateWrapper.set("com_name", comName);
        } else {
            updateWrapper.set("com_name", null);
        }
        if (!StringUtils.isEmpty(comCode)) {
            updateWrapper.set("com_code", comCode);
        } else {
            updateWrapper.set("com_code", null);
        }
        int update = alarmComMapper.update(updateWrapper);
        if (update > 0) {
            AlarmCombinationRDS alarmCombinationRDS = new AlarmCombinationRDS();
            BeanUtils.copyProperties(alarmCombination, alarmCombinationRDS);
            jedisClusterBSJ.hset(RedisConstant.ALARM_COMBINATION
                    , alarmCombination.getId() + "", JSON.toJSONString(alarmCombinationRDS));
        }

    }

    @Override
    public void alarmComDelete(Integer id) {
        int count = alarmComMapper.deleteById(id);
        if (count > 0) {
            jedisClusterBSJ.hdel(RedisConstant.ALARM_COMBINATION
                    , id + "");
        }
    }

    @Override
    public JsonResult<List<AlarmCombinationVO>> listAlarmCom(Integer pageNumber, Integer pageSize, String comName) {
        IPage<AlarmCombination> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<AlarmCombination> queryWrapper = null;
        if (!StringUtils.isEmpty(comName)) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("com_name", '%' + comName + '%');
        }
        IPage<AlarmCombination> iPage = alarmComMapper.selectPage(page, queryWrapper);
        List<AlarmCombination> records = iPage.getRecords();
        List<AlarmCombinationVO> vos = new ArrayList<>();
        for (AlarmCombination record : records) {
            AlarmCombinationVO vo = new AlarmCombinationVO();
            BeanUtils.copyProperties(record, vo);
            vos.add(vo);
        }
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setData(vos);
        jsonResult.setTotal(iPage.getTotal());
        return jsonResult;
    }

    @Override
    public IPage<AlarmReport> abnormalOfflineAlarmList(AlarmQueryDTO alarmQueryDTO) {
        String deviceId = alarmQueryDTO.getDeviceId();
        String nickName = alarmQueryDTO.getNickName();
        String beginTime = alarmQueryDTO.getBeginTime();
        String endTime = alarmQueryDTO.getEndTime();
        Integer pageNumber = alarmQueryDTO.getPageNumber();
        Integer pageSize = alarmQueryDTO.getPageSize();

        IPage<AlarmReport> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<AlarmReport> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(deviceId)) {
            queryWrapper.like("device_id", "%" + deviceId + "%");
        }
        if (StringUtils.isNotEmpty(nickName)) {
            queryWrapper.like("nick_name", "%" + nickName + "%");
        }
        queryWrapper.eq("alarm_type", AlarmEnum.DEVICE_ABNORMAL_OFFLINE.getCode());
        queryWrapper.between("begin_time", beginTime, endTime);
        queryWrapper.orderByDesc("begin_time");

        return alarmReportMapper.selectPage(page, queryWrapper);
    }

    /**
     * 处理报警
     *
     * @param alarmHandleDTO
     * @return com.bsj.power.common.config.global.JsonResult
     * @author ljx
     * @time 2024/5/23 16:02
     */
    @Override
    @Transactional
    public JsonResult handleAlarm(AlarmHandleDTO alarmHandleDTO) {
        log.info("请求参数{}", alarmHandleDTO);
        Long alarmId = alarmHandleDTO.getAlarmId();
        Integer handleWay = alarmHandleDTO.getHandleWay();
        AlarmReport oldAlarmReport = alarmReportMapper.selectById(alarmId);
        if (oldAlarmReport == null) {
            return JsonResult.fail(JsonResultEnum.ALARM_NOT_EXIST);
        }
        //不属于围栏报警不允许处理
//        if (!alarmTypeList.contains(oldAlarmReport.getAlarmType())) {
//            return JsonResult.fail(JsonResultEnum.ALARM_NOT_EXIST);
//        }
        String deviceName = oldAlarmReport.getDeviceName();
//        Integer pathId = oldAlarmReport.getPathId();

        //结束报警
        Date date = new Date();
        AlarmReport alarmReport = new AlarmReport();
//        AlarmReport alarmCCached = pathAlarmCached.hgetPathAlarmInfo(deviceName, pathId);
        if (oldAlarmReport.getEndTime() == null) {
            alarmReport.setEndTime(date);
        }
        alarmReport.setAlarmId(alarmId);
        alarmReport.setHandleWay(handleWay);
        alarmReport.setAlarmHandleTime(date);
        alarmReport.setRemark(alarmHandleDTO.getRemark());
        alarmReport.setHandleState(NumericalConstants.TWO);
        alarmReport.setEventDescription(alarmHandleDTO.getEventDescription());
        alarmReport.setResponseMeasures(alarmHandleDTO.getResponseMeasures());
        if (alarmReportMapper.updateById(alarmReport) > NumericalConstants.ZERO) {
//            pathAlarmCached.hdelPathAlarmInfo(deviceName, pathId);
            return JsonResult.success();
        } else {
            return JsonResult.fail();
        }
    }

}
